package com.ruoyi.jr.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.jr.config.Result;
import com.ruoyi.jr.domain.JrProduct;
import com.ruoyi.jr.domain.JrRentInstallmentResponseDto;
import com.ruoyi.jr.domain.JrRentInstallmentSearchPage;
import com.ruoyi.jr.service.JrProductService;
import com.ruoyi.jr.service.JrRentInstallmentService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("product")
@RestController
public class JrProductController {
    @Resource
    private JrProductService jrProductService;


    @Resource
    private JrRentInstallmentService jrRentInstallmentService;

    @RequestMapping("/list")
    public IPage<JrProduct> list(Page page, Integer jrProductType){
        return jrProductService.selectPageVo(page,jrProductType);
    }


    @RequestMapping("/updete")
    public Result updete(JrProduct jrProduct){
        boolean b = jrProductService.updateById(jrProduct);
        System.out.println(b);
        return b?Result.success():Result.fail();
    }




    @PostMapping("searchRentInstall")
    public Map searchRentInstall(@RequestBody JrRentInstallmentSearchPage jrRentInstallmentSearchPage)  {
        Map map = new HashMap();
        PageHelper.startPage(jrRentInstallmentSearchPage.getPageNum(), jrRentInstallmentSearchPage.getPageSize());
        List<JrRentInstallmentResponseDto> list = jrRentInstallmentService.searchLoans(jrRentInstallmentSearchPage);
        PageInfo pageInfo = new PageInfo(list);
        map.put("list",pageInfo.getList());
        System.err.println(pageInfo.getList());
        map.put("total",pageInfo.getTotal());
        map.put("pageSize",pageInfo.getPageSize());
        map.put("pageNum",pageInfo.getPageNum());
        return map;
    }


    @RequestMapping("/jrList")
    public IPage<JrProduct> JrProductList(Page page, JrRentInstallmentSearchPage searchPage){
        return jrRentInstallmentService.selectPageVo(page,searchPage);
    }
}
